{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting Pillow\n",
      "  Downloading pillow-11.0.0-cp311-cp311-win_amd64.whl.metadata (9.3 kB)\n",
      "Downloading pillow-11.0.0-cp311-cp311-win_amd64.whl (2.6 MB)\n",
      "   ---------------------------------------- 0.0/2.6 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.6 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.6 MB 222.6 kB/s eta 0:00:12\n",
      "   ---------------------------------------- 0.0/2.6 MB 262.6 kB/s eta 0:00:10\n",
      "   ---------------------------------------- 0.0/2.6 MB 262.6 kB/s eta 0:00:10\n",
      "    --------------------------------------- 0.0/2.6 MB 196.9 kB/s eta 0:00:13\n",
      "    --------------------------------------- 0.1/2.6 MB 252.2 kB/s eta 0:00:10\n",
      "   - -------------------------------------- 0.1/2.6 MB 309.1 kB/s eta 0:00:09\n",
      "   - -------------------------------------- 0.1/2.6 MB 364.4 kB/s eta 0:00:07\n",
      "   -- ------------------------------------- 0.1/2.6 MB 370.8 kB/s eta 0:00:07\n",
      "   --- ------------------------------------ 0.2/2.6 MB 437.8 kB/s eta 0:00:06\n",
      "   --- ------------------------------------ 0.3/2.6 MB 542.5 kB/s eta 0:00:05\n",
      "   ---- ----------------------------------- 0.3/2.6 MB 568.9 kB/s eta 0:00:05\n",
      "   ----- ---------------------------------- 0.3/2.6 MB 600.1 kB/s eta 0:00:04\n",
      "   ------ --------------------------------- 0.4/2.6 MB 656.2 kB/s eta 0:00:04\n",
      "   ------- -------------------------------- 0.5/2.6 MB 768.0 kB/s eta 0:00:03\n",
      "   -------- ------------------------------- 0.5/2.6 MB 796.1 kB/s eta 0:00:03\n",
      "   --------- ------------------------------ 0.6/2.6 MB 809.2 kB/s eta 0:00:03\n",
      "   ---------- ----------------------------- 0.7/2.6 MB 839.1 kB/s eta 0:00:03\n",
      "   ----------- ---------------------------- 0.8/2.6 MB 933.2 kB/s eta 0:00:02\n",
      "   ------------ --------------------------- 0.8/2.6 MB 935.3 kB/s eta 0:00:02\n",
      "   ------------- -------------------------- 0.9/2.6 MB 938.1 kB/s eta 0:00:02\n",
      "   -------------- ------------------------- 0.9/2.6 MB 967.1 kB/s eta 0:00:02\n",
      "   ------------------ --------------------- 1.2/2.6 MB 1.1 MB/s eta 0:00:02\n",
      "   -------------------- ------------------- 1.3/2.6 MB 1.3 MB/s eta 0:00:01\n",
      "   ------------------------ --------------- 1.5/2.6 MB 1.4 MB/s eta 0:00:01\n",
      "   -------------------------- ------------- 1.7/2.6 MB 1.5 MB/s eta 0:00:01\n",
      "   -------------------------------- ------- 2.1/2.6 MB 1.8 MB/s eta 0:00:01\n",
      "   -------------------------------------- - 2.5/2.6 MB 2.0 MB/s eta 0:00:01\n",
      "   ---------------------------------------- 2.6/2.6 MB 2.0 MB/s eta 0:00:00\n",
      "Installing collected packages: Pillow\n",
      "Successfully installed Pillow-11.0.0\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip is available: 24.0 -> 24.3.1\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    }
   ],
   "source": [
    "pip install Pillow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import tkinter\n",
    "import tkinter.simpledialog\n",
    "import tkinter.colorchooser\n",
    "import tkinter.filedialog\n",
    "from PIL import ImageGrab  # 确保已安装Pillow库，使用pip install pillow\n",
    "\n",
    "root = tkinter.Tk()\n",
    "root.title('MyPaint----byDongFuguo')\n",
    "root.geometry('800x600')  # 使用geometry代替直接设置width和height\n",
    "\n",
    "# 控制是否允许画图的变量，1：允许，0：不允许\n",
    "canDraw = tkinter.IntVar(value=0)\n",
    "\n",
    "# 控制画图类型的变量，1：曲线，2：直线，3：矩形，4：文本，5：橡皮，6：圆形\n",
    "what = tkinter.IntVar(value=1)\n",
    "\n",
    "# 记录鼠标位置的变量\n",
    "X = tkinter.IntVar(value=0)\n",
    "Y = tkinter.IntVar(value=0)\n",
    "\n",
    "# 文本输入的全局变量\n",
    "text = \"\"\n",
    "\n",
    "# 前景色、背景色\n",
    "foreColor = '#000000'\n",
    "backColor = '#FFFFFF'\n",
    "\n",
    "# 创建画布\n",
    "canvas = tkinter.Canvas(root, bg='white', width=800, height=600)\n",
    "canvas.pack(fill=tkinter.BOTH, expand=tkinter.YES)\n",
    "\n",
    "# 鼠标左键单击，允许画图，并记录当前鼠标位置\n",
    "def onLeftButtonDown(event):\n",
    "    canDraw.set(1)\n",
    "    X.set(event.x)\n",
    "    Y.set(event.y)\n",
    "    if what.get() == 4:\n",
    "        global text\n",
    "        text = tkinter.simpledialog.askstring(title='Input what you want to draw', prompt='')\n",
    "        if text:\n",
    "            canvas.create_text(event.x, event.y, text=text, fill=foreColor)\n",
    "\n",
    "canvas.bind('<Button-1>', onLeftButtonDown)\n",
    "\n",
    "# 记录最后绘制图形的id\n",
    "lastDraw = None\n",
    "\n",
    "# 按住鼠标左键移动，画图\n",
    "def onLeftButtonMove(event):\n",
    "    global lastDraw\n",
    "    if canDraw.get() == 0:\n",
    "        return\n",
    "    if what.get() == 1:\n",
    "        canvas.create_line(X.get(), Y.get(), event.x, event.y, fill=foreColor, smooth=True)  # 添加smooth=True使曲线更平滑\n",
    "        X.set(event.x)\n",
    "        Y.set(event.y)\n",
    "    elif what.get() == 2:\n",
    "        if lastDraw:\n",
    "            canvas.delete(lastDraw)\n",
    "        lastDraw = canvas.create_line(X.get(), Y.get(), event.x, event.y, fill=foreColor)\n",
    "        X.set(event.x)\n",
    "        Y.set(event.y)\n",
    "    elif what.get() == 3:\n",
    "        if lastDraw:\n",
    "            canvas.delete(lastDraw)\n",
    "        lastDraw = canvas.create_rectangle(X.get(), Y.get(), event.x, event.y, outline=foreColor, fill=backColor)\n",
    "    elif what.get() == 6:  # 圆形\n",
    "        if lastDraw:\n",
    "            canvas.delete(lastDraw)\n",
    "        radius = ((event.x - X.get()) ** 2 + (event.y - Y.get()) ** 2) ** 0.5\n",
    "        lastDraw = canvas.create_oval(X.get() - radius, Y.get() - radius, X.get() + radius, Y.get() + radius, outline=foreColor, fill=backColor)\n",
    "    elif what.get() == 5:\n",
    "        canvas.create_rectangle(event.x - 5, event.y - 5, event.x + 5, event.y + 5, outline=backColor, fill=backColor)\n",
    "\n",
    "canvas.bind('<B1-Motion>', onLeftButtonMove)\n",
    "\n",
    "# 鼠标左键抬起，不允许再画图了\n",
    "def onLeftButtonUp(event):\n",
    "    if what.get() in (2, 3, 6):  # 只有直线、矩形和圆形需要在抬起时完成绘制（对于圆形，我们需要在抬起时确定最终大小）\n",
    "        if lastDraw:\n",
    "            if what.get() == 2:\n",
    "                canvas.create_line(X.get(), Y.get(), event.x, event.y, fill=foreColor)\n",
    "            elif what.get() == 3:\n",
    "                canvas.create_rectangle(X.get(), Y.get(), event.x, event.y, outline=foreColor, fill=backColor)\n",
    "            elif what.get() == 6:  # 圆形在抬起时不需要额外操作，因为已经在移动中绘制完成\n",
    "                pass\n",
    "            lastDraw = None  # 重置lastDraw\n",
    "    canDraw.set(0)\n",
    "\n",
    "canvas.bind('<ButtonRelease-1>', onLeftButtonUp)\n",
    "\n",
    "# 创建菜单\n",
    "menu = tkinter.Menu(root, tearoff=0)\n",
    "\n",
    "# 打开图像文件\n",
    "def Open():\n",
    "    filename = tkinter.filedialog.askopenfilename(title='Open Image', filetypes=[('Image Files', '*.jpg;*.png;*.gif')])\n",
    "    if filename:\n",
    "        img = tkinter.PhotoImage(file=filename)\n",
    "        canvas.create_image(400, 300, image=img, anchor=tkinter.CENTER)  # 居中显示\n",
    "        img.image = img  # 保持对图片的引用\n",
    "\n",
    "menu.add_command(label='Open', command=Open)\n",
    "\n",
    "def Save():\n",
    "    left = int(root.winfo_rootx())\n",
    "    top = int(root.winfo_rooty())\n",
    "    width = root.winfo_width()\n",
    "    height = root.winfo_height()\n",
    "    im = ImageGrab.grab((left, top, left + width, top + height))\n",
    "    filename = tkinter.filedialog.asksaveasfilename(title='Save Image', filetypes=[('PNG Files', '*.png')])\n",
    "    if filename:\n",
    "        im.save(filename)\n",
    "\n",
    "menu.add_command(label='Save', command=Save)\n",
    "\n",
    "# 添加菜单，清除，遍历画布上的所有图像，逐个删除\n",
    "def Clear():\n",
    "    for item in canvas.find_all():\n",
    "        canvas.delete(item)\n",
    "\n",
    "menu.add_command(label='Clear', command=Clear)\n",
    "\n",
    "# 添加分割线\n",
    "menu.add_separator()\n",
    "\n",
    "# 创建子菜单，用来选择绘图类型\n",
    "menuType = tkinter.Menu(menu, tearoff=0)\n",
    "\n",
    "def drawCurve():\n",
    "    what.set(1)\n",
    "\n",
    "menuType.add_command(label='Curve', command=drawCurve)\n",
    "\n",
    "def drawLine():\n",
    "    what.set(2)\n",
    "\n",
    "menuType.add_command(label='Line', command=drawLine)  # 修正了这里的拼写错误\n",
    "\n",
    "def drawRectangle():\n",
    "    what.set(3)\n",
    "\n",
    "menuType.add_command(label='Rectangle', command=drawRectangle)\n",
    "\n",
    "def drawText():\n",
    "    global text\n",
    "    text = tkinter.simpledialog.askstring(title='Input what you want to draw', prompt='')\n",
    "    what.set(4)\n",
    "    if text:  # 只有在输入文本后才设置what为4\n",
    "        canvas.create_text(X.get(), Y.get(), text=text, fill=foreColor)  # 立即显示文本\n",
    "\n",
    "menuType.add_command(label='Text', command=drawText)\n",
    "\n",
    "def drawCircle():\n",
    "    what.set(6)\n",
    "\n",
    "menuType.add_command(label='Circle', command=drawCircle)\n",
    "\n",
    "# 选择前景色\n",
    "def chooseForeColor():\n",
    "    global foreColor\n",
    "    foreColor = tkinter.colorchooser.askcolor()[1]\n",
    "\n",
    "menuType.add_command(label='Choose Foreground Color', command=chooseForeColor)\n",
    "\n",
    "# 选择背景色\n",
    "def chooseBackColor():\n",
    "    global backColor\n",
    "    backColor = tkinter.colorchooser.askcolor()[1]\n",
    "\n",
    "menuType.add_command(label='Choose Background Color', command=chooseBackColor)\n",
    "\n",
    "# 橡皮\n",
    "def onErase():\n",
    "    what.set(5)\n",
    "\n",
    "menuType.add_command(label='Erase', command=onErase)\n",
    "menu.add_cascade(label='Type', menu=menuType)\n",
    "\n",
    "# 鼠标右键抬起，弹出菜单\n",
    "def onRightButtonUp(event):\n",
    "    menu.post(event.x_root, event.y_root)\n",
    "\n",
    "canvas.bind('<ButtonRelease-3>', onRightButtonUp)\n",
    "\n",
    "# 启动应用程序\n",
    "root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
